package cn.gcjxsc.controller.api.salesman;

import cn.gcjxsc.common.util.validator.BindResult;
import cn.gcjxsc.consts.ErrorConst;
import cn.gcjxsc.consts.WithdrawStatus;
import cn.gcjxsc.data.query.common.WithdrawQuery;
import cn.gcjxsc.data.vo.WithdrawVo;
import cn.gcjxsc.entity.account.Withdraw;
import cn.gcjxsc.entity.salesman.BankCard;
import cn.gcjxsc.entity.salesman.Salesman;
import cn.gcjxsc.service.account.WithdrawService;
import cn.gcjxsc.service.integral.BankCardService;
import cn.gcjxsc.service.integral.SalesmanService;
import com.zgxl.common.util.AjaxResponse;
import com.zgxl.common.vo.PageDataVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author pumbaa
 * @since 1.0
 */
@RestController("apiSalesmanWithdrawController")
@RequestMapping("/api/salesman")
public class WithdrawController{
    @Autowired
    private SalesmanService salesmanService;
    @Autowired
    private WithdrawService withdrawService;
    @Autowired
    private BankCardService bankCardService;

    /**
     * 根据id查询提现信息
     *
     * @param id 提现id
     * @return
     */
    @RequestMapping("/withdraw/find")
    public WithdrawVo findById(@RequestParam("id") Integer id,
                               @RequestParam("type") String type) {
        Withdraw withdraw = withdrawService.findById(id);
        if (null == withdraw || !withdraw.getWithdrawType().name().equalsIgnoreCase(type)) {
            return null;
        }
        return withdrawService.convertWithdraw(withdraw);
    }


    @RequestMapping("/withdraw/list")
    public PageDataVo<WithdrawVo> queryList(WithdrawQuery filter,
                                            @RequestParam(defaultValue = "1", required = false) int page,
                                            @RequestParam(defaultValue = "30", required = false) int pageSize) {
        filter.format();
        Page<Withdraw> pageList = withdrawService.findAll(filter, page, pageSize);
        List<Withdraw> listContent = pageList.getContent();
        List<WithdrawVo> voList = new ArrayList<>();
        for (Withdraw withdraw : listContent) {
            voList.add(withdrawService.convertWithdraw(withdraw));
        }
        PageDataVo<WithdrawVo> dataVo = new PageDataVo<>();
        dataVo.setList(voList);
        dataVo.setPage(page);
        dataVo.setPageSize(pageSize);
        dataVo.setTotal(pageList.getTotalElements());
        return dataVo;
    }

    /**
     * 业务员申请提现
     *
     * @param salesmanId 业务员id
     * @return
     */
    @RequestMapping(value = "/withdraw/apply", method = RequestMethod.POST)
    public AjaxResponse applyWithdraw(@RequestParam("id") Integer salesmanId,
                                      @RequestParam("amount") BigDecimal amount,
                                      @RequestParam("bankId") Integer bankId) {
        if (amount.compareTo(BigDecimal.ZERO) < 1) {
            return AjaxResponse.ajaxFailure("请输入有效金额");
        }
        Salesman salesman = salesmanService.findById(salesmanId);
        if (null == salesman) {
            return AjaxResponse.ajaxFailure(ErrorConst.RECORD_NOT_EXIST);
        }
        BankCard bankCard = bankCardService.findById(bankId);
        if (null == bankCard) {
            return AjaxResponse.ajaxFailure("无效银行卡信息");
        }
        BindResult bindResult = new BindResult();
        Withdraw withdraw = withdrawService.salesmanApply(salesman, amount, bankCard, bindResult);
        if (null == withdraw) {
            return AjaxResponse.ajaxFailure(bindResult.getErrorMsg());
        }
        return AjaxResponse.ajaxSuccess("", withdraw.getSn());
    }

    /**
     * 业务员取消提现申请
     *
     * @param withdrawId 提现id
     * @param salesmanId 业务员id
     * @return
     */
    @RequestMapping(value = "/withdraw/cancel", method = RequestMethod.POST)
    public AjaxResponse cancelWithdraw(@RequestParam("id") Integer withdrawId,
                                    @RequestParam("salesmanId") Integer salesmanId) {
        Salesman salesman = salesmanService.findById(salesmanId);
        if (null == salesman) {
            return AjaxResponse.ajaxFailure(ErrorConst.RECORD_NOT_EXIST);
        }
        Withdraw withdraw = withdrawService.findById(withdrawId);
        if (null == withdraw) {
            return AjaxResponse.ajaxFailure(ErrorConst.RECORD_NOT_EXIST);
        } else if (withdraw.getWithdrawStatus() != WithdrawStatus.WAIT) {
            return AjaxResponse.ajaxFailure("当前状态不允许该操作");
        } else if (!Objects.equals(salesman.getId(), withdraw.getExtraId())) {
            return AjaxResponse.ajaxFailure(ErrorConst.ILLEGAL_OPERATION);
        }
        try {
            withdrawService.cancelSalesmanWithdraw(withdraw);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResponse.ajaxFailure("处理失败,请稍后再试");
        }
        return AjaxResponse.ajaxSuccess();
    }

    /**
     * 通过提现申请
     *
     * @param withdrawId 提现id
     * @param username   操作人员
     * @return
     */
    @RequestMapping(value = "/withdraw/pass")
    public AjaxResponse passWithdraw(@RequestParam("id") Integer withdrawId,
                                  @RequestParam("username") String username) {
        Withdraw withdraw = withdrawService.findById(withdrawId);
        if (null == withdraw) {
            return AjaxResponse.ajaxFailure(ErrorConst.RECORD_NOT_EXIST);
        } else if (withdraw.getWithdrawStatus() != WithdrawStatus.WAIT) {
            return AjaxResponse.ajaxFailure("当前状态不允许该操作");
        }
        try {
            withdrawService.passWithdraw(withdraw, username);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResponse.ajaxFailure("处理失败,请稍后再试");
        }
        return AjaxResponse.ajaxSuccess();
    }

    /**
     * 完成提现
     *
     * @param withdrawId 提现id
     * @param username   操作人员
     * @return
     */
    @RequestMapping(value = "/withdraw/done")
    public AjaxResponse doneWithdraw(@RequestParam("id") Integer withdrawId,
                                  @RequestParam("username") String username) {
        Withdraw withdraw = withdrawService.findById(withdrawId);
        if (null == withdraw) {
            return AjaxResponse.ajaxFailure(ErrorConst.RECORD_NOT_EXIST);
        } else if (withdraw.getWithdrawStatus() != WithdrawStatus.PASSED) {
            return AjaxResponse.ajaxFailure("当前状态不允许该操作");
        }
        try {
            withdrawService.doneWithdraw(withdraw, username);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResponse.ajaxFailure("处理失败,请稍后再试");
        }
        return AjaxResponse.ajaxSuccess();
    }

    /**
     * 拒绝提现
     *
     * @param withdrawId 提现id
     * @param username   操作人员
     * @return
     */
    @RequestMapping(value = "/withdraw/reject")
    public AjaxResponse rejectWithdraw(@RequestParam("id") Integer withdrawId,
                                    @RequestParam("username") String username,
                                    @RequestParam("reason") String reason) {
        Withdraw withdraw = withdrawService.findById(withdrawId);
        if (null == withdraw) {
            return AjaxResponse.ajaxFailure(ErrorConst.RECORD_NOT_EXIST);
        } else if (withdraw.getWithdrawStatus() != WithdrawStatus.WAIT) {
            return AjaxResponse.ajaxFailure("当前状态不允许该操作");
        }
        try {
            withdrawService.rejectSalesmanWithdraw(withdraw, username, reason);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResponse.ajaxFailure("处理失败,请稍后再试");
        }
        return AjaxResponse.ajaxSuccess();
    }
}
